package fetcher

import (
	"fmt"
	"io/ioutil"
	"log"
	"my-crawler/login"
	"net/http"
	"time"
)

const filename = "login/token.txt"

func ReadContext(filename string) string {
	data, _ := ioutil.ReadFile(filename)
	return string(data)
}

func FetchWithCookie(url string) ([]byte, error) {
	client := http.Client{}
	req, _ := http.NewRequest(http.MethodGet, url, nil)
	//打印时间
	fmt.Println(time.Now().Unix())
	//从文件中获取token
	var token = ReadContext(filename)
	cookie1 := "reborn-userToken=" + token // 找到的cookie
	req.Header.Add("cookie", cookie1)
	req.Header.Add("User-Agent", "Baiduspider")
	resp, err := client.Do(req)
	if resp == nil {
		log.Println("resp is nil: ", req)
		return nil, err
	}
	if resp.StatusCode == http.StatusBadRequest {
		//cookie 问题 重新获取
		login.Login()
		cookie1 = "reborn-userToken=" + ReadContext(filename)
		req.Header.Set("cookie", cookie1)
		resp, err = client.Do(req)
	}
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	//不要头部的信息 只需要判code信息即可
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("error:status code %d", resp.StatusCode) //fmt.Errorf()
	}

	all, err := ioutil.ReadAll(resp.Body)
	return all, nil
}
